<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="74px">
      <el-form-item label="打印人员" prop="printer">
        <el-select v-model="queryParams.printer" placeholder="请输入3D打印人员" clearable size="small">
          <el-option
            v-for="dict in dict.type.nonmetal_3Dprinter"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="前处理人" prop="dataPreprocessor">
        <el-select v-model="queryParams.dataPreprocessor" placeholder="请输入前处理人员" clearable size="small">
          <el-option
            v-for="dict in dict.type.nonmetal_preprocess_prep"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
<!--        <el-select v-model="queryParams.dataPreprocessor" multiple @change="$forceUpdate()" placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="item in dict.type.nonmetal_preprocess_prep"-->
<!--            :key="parseInt(item.value)"-->
<!--            :label="item.label"-->
<!--            :value="parseInt(item.value)"-->
<!--            :disabled="false"-->
<!--          ></el-option>-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理结果" prop="result">
        <el-select v-model="queryParams.result" placeholder="请选择处理结果" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否处理" prop="isHandle">
        <el-select v-model="queryParams.isHandle" placeholder="请选择是否处理" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_is_handling"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['cmenonmetal:preprocess:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cmenonmetal:preprocess:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['cmenonmetal:preprocess:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['cmenonmetal:preprocess:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="preprocessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="3D打印人员" align="center" prop="printer" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.nonmetal_3Dprinter" :value="scope.row.printer"/>
        </template>
      </el-table-column>
      <el-table-column label="前处理人员" align="center" prop="dataPreprocessor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.nonmetal_preprocess_prep" :value="scope.row.dataPreprocessor"/>
        </template>
      </el-table-column>

      <el-table-column label="前处理记录" align="center" prop="prePorcessingRecord" />
      <el-table-column label="处理结果" align="center" prop="result">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_result" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center" prop="isHandle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_handling" :value="scope.row.isHandle"/>
        </template>
      </el-table-column>
      <el-table-column label="其他" align="center" prop="other" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cmenonmetal:preprocess:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="todetail1(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改前处理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="87px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" :disabled="true" />
        </el-form-item>
        <el-form-item label="3D打印人员" prop="printer">
          <el-select v-model="form.printer" placeholder="请输入3D打印人员">
            <el-option
              v-for="dict in dict.type.nonmetal_3Dprinter"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="前处理人员" prop="dataPreprocessor">
          <el-select v-model="form.dataPreprocessor" placeholder="请输入前处理人员">
            <el-option
              v-for="dict in dict.type.nonmetal_preprocess_prep"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>

          </el-form-item>



        <el-form-item label="前处理记录" prop="prePorcessingRecord">
          <el-input v-model="form.prePorcessingRecord" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理结果">
          <el-radio-group v-model="form.result">
            <el-radio
              v-for="dict in dict.type.sys_result"
              :key="dict.value"
:label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否处理">
          <el-radio-group v-model="form.isHandle">
            <el-radio
              v-for="dict in dict.type.sys_is_handling"
              :key="dict.value"
:label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="其他" prop="other">
          <el-input v-model="form.other" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="上传">
          <!--<file-upload/>-->
          <preprocess-upload :orderid="form.orderId" />
        </el-form-item>
        <el-form-item label="下载">
          <el-link   :underline="false">
            <el-button type="primary" @click="getpreprocessfile" >获取文件<i class="el-icon-download el-icon--right"></i></el-button>
          </el-link>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPreprocess, getPreprocess, delPreprocess, addPreprocess, updatePreprocess } from "@/api/cmenonmetalapi/preprocess";
import PreprocessUpload from "@/views/cmenonmetal/file/PreprocessUpload";
import {getFileInformation,listZsorderurl} from "@/api/cmenonmetalapi/zsorderurl";
import {download} from "@/utils/request";


export default {
  name: "Preprocess",
  dicts: ['sys_result', 'sys_is_handling','nonmetal_preprocess_prep','nonmetal_3Dprinter'],
  components: {
    PreprocessUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 前处理表格数据
      preprocessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //文件url
      urlList:{},
      //前处理文件名
      predatafilename: null,
      //前处理文件路径
      predataurl: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prePorcessingRecord: null,
        printer: null,
        dataPreprocessor: null,
        orderId: null,
        result: null,
        isHandle: null,
        other: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        result: [
          { required: true, message: "处理结果不能为空", trigger: "blur" }
        ],
        isHandle: [
          { required: true, message: "是否处理不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询前处理列表 */
    getList() {
      this.loading = true;
      listPreprocess(this.queryParams).then(response => {
        this.preprocessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    /** 跳转到详情页 */
    todetail1(row) {
      const orderid = row.orderId
      this.$router.push({ path: '/Detail1', query: {orderid:orderid} })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        prePorcessingRecord: null,
        printer: null,
        dataPreprocessor: null,
        orderId: null,
        oederid: null,
        result: 0,
        isHandle: 0,
        other: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加前处理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPreprocess(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改前处理";

        //获取url原始数据
        this.queryParams.oederid = this.form.orderId;
        this.queryParams.orderId = this.form.orderId;
        listZsorderurl(this.queryParams).then(response => {
          this.urlList = response.rows[0];
        });
      });

      //获取文件信息
      getFileInformation(this.queryParams).then(response => {
        this.predatafilename = response.predatafilename;
        this.predataurl = response.predataurl;
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePreprocess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreprocess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      this.queryParams.orderId = null;
    },
    //datapreprocess文件下载
    getpreprocessfile(){
      this.queryParams.resource = this.urlList.perprocessUpload;
      download("/cmenonmetal/download/resource", this.queryParams, "非金属前处理文件"+this.queryParams.orderId + this.predatafilename);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除前处理编号为"' + ids + '"的数据项？').then(function() {
        return delPreprocess(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cmenonmetal/preprocess/export', {
        ...this.queryParams
      }, `preprocess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
